package org.gten.gegedexin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.gten.gegedexin.Data.Draft;
import org.gten.gegedexin.pojo.DraftPojo;
import org.gten.gegedexin.service.DraftServiceImpl;
import org.gten.gegedexin.service.ReadDraft;
import org.gten.gegedexin.service.ReadDraftList;
import org.gten.gegedexin.service.StoreDraft;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
public class DraftsController {
    @Autowired
    ReadDraftList readDraftList;
    @Autowired
    StoreDraft storeDraft;
    @Autowired
    ReadDraft readDraft;

    @PostMapping("/receiveDraft")
    public void receiveDraft(Draft draft){
        storeDraft.store(draft);
    }

    //  获取用户的草稿箱列表，返回草稿箱总页数
    @RequestMapping("/getDraftList")
    @CrossOrigin
    public int getDraftList(String sender){
        readDraftList.readList(sender);
        return readDraftList.returnPage();
    }

    //  返回草稿箱指定页的邮件列表
    @RequestMapping("/getDraftPage")
    @ResponseBody
    @CrossOrigin
    public JSONArray getDraftPage(int page){
        return readDraftList.readPage(page);
    }

    //  根据id返回指定草稿
    @RequestMapping("/getDraft")
    public JSON getDraft(String ID){
        return readDraft.readDraft(ID);
    }

}